package com.component;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.faces.context.FacesContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.Inject;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.SaveState;
import org.operamasks.faces.component.tree.base.TreeDataProvider;
import org.operamasks.faces.component.tree.impl.UITree;
import org.operamasks.faces.component.tree.impl.UITreeNode;
import org.operamasks.faces.event.ajax.TreeNodeEvent;
import org.operamasks.faces.user.ajax.PartialUpdateManager;

import com.base.commonKey.entity.CommonKey;
import com.base.commonKey.service.ICommonKeyService;
import com.base.commons.SpringBeanHolder;
import com.base.utils.DataStructureUtils;

/**
 * 
 * This managed bean is generated automatically
 */
@ManagedBean(name = "component_commonKeyTreeBean", scope = ManagedBeanScope.SESSION)
public class CommonKeyTreeBean implements Serializable {
	private final static Log logger = LogFactory
			.getLog(CommonKeyTreeBean.class);
	private ICommonKeyService commonKeyService = SpringBeanHolder
			.getBean("commonKeyService");
	@Inject
	private FacesContext context;
	@Inject
	private PartialUpdateManager update;
	@Bind(id = "ck_txt", attribute = "value")
	private String ck_txt;
	@Bind(id = "selectedText", attribute = "value")
	private String selectedText;
	@Bind(id = "selectedId", attribute = "value")
	private String selectedId;
	// 搜索map
	private Map<String, Set<Integer>> searchMap;
	@SaveState
	private String keyType;
	@Bind
	private UITree cks_selectTree;
	@Bind(id = "cks_selectTree")
	private TreeDataProvider treeData_cks_selectTree = new TreeDataProvider() {

		public Object[] getChildren(Object node) {
			logger.debug("keyType in treeProvider is:" + keyType);
			// 搜索
			if (ck_txt != null && !"".equals(ck_txt)) {
				if (node == null) {
					return new String[] { "通用选项" };
				}
				if ("通用选项".equals(node.toString())) {
					Set<Integer> searchIds = searchMap
							.get(ICommonKeyService.SEARCHMAP_KEY);
					List<CommonKey> temp = commonKeyService.getTopByIds(
							keyType, DataStructureUtils.setToList(searchIds));
					logger.debug("top size:" + temp.size());
					return temp.toArray();
				}
				if (node instanceof CommonKey) {
					Set<Integer> searchIds = searchMap
							.get(ICommonKeyService.SEARCHMAP_KEY);
					CommonKey ck = (CommonKey) node;
					List<CommonKey> sub = commonKeyService.getChildenByIds(ck
							.getId(), DataStructureUtils.setToList(searchIds));
					logger.debug("sub size:" + sub.size());
					if (!sub.isEmpty())
						return sub.toArray();
				}
			} else {
				if (node == null) {
					return new String[] { "通用选项" };
				}
				if ("通用选项".equals(node.toString())) {
					List<CommonKey> temp = commonKeyService.getTopAll(keyType);
					logger.debug("top size:" + temp.size());
					return temp.toArray();
				}
				if (node instanceof CommonKey) {
					CommonKey ck = (CommonKey) node;
					List<CommonKey> sub = commonKeyService.getChilden(ck
							.getId());
					logger.debug("sub size:" + sub.size());
					if (!sub.isEmpty())
						return sub.toArray();
				}
			}
			return null;
		}

		public String getHref(Object nodeData) {
			return null;
		}

		public String getHrefTarget(Object nodeData) {
			return null;
		}

		public String getIcon(Object nodeData) {
			if (nodeData instanceof CommonKey) {
				CommonKey ck = (CommonKey) nodeData;
				if (ck.isCheckEnable())
					return FacesContext.getCurrentInstance()
							.getExternalContext().getRequestContextPath()
							+ "/images/listshow.gif";
				return FacesContext.getCurrentInstance().getExternalContext()
						.getRequestContextPath()
						+ "/images/folder.png";
			}
			return "";
		}

		public String getText(Object nodeData) {
			if (nodeData == null)
				return null;
			if (nodeData instanceof CommonKey)
				return ((CommonKey) nodeData).getKeyValue();
			return nodeData.toString();
		}

		public boolean isCascade(Object nodeData) {
			return false;
		}

		public Boolean isChecked(Object nodeData) {
			return null;
		}

		public boolean isExpanded(Object nodeData) {
			if (nodeData == null)
				return true;
			if (nodeData instanceof CommonKey) {
				CommonKey ck = (CommonKey) nodeData;
				if (ck.getChildren() == null)
					return true;
				if (ck.getChildren().isEmpty())
					return true;
			}
			return false;
		}

		public boolean isLeaf(Object nodeData) {
			return false;
		}
	};

	@Action(id = "cks_selectTree", event = "onclick")
	public void click(TreeNodeEvent event) {
		UITreeNode node = event.getEventNode();
		Object data = node.getUserData();
		if (data == null)
			return;
		if (data instanceof CommonKey) {
			CommonKey ck = (CommonKey) data;
			if (ck.isCheckEnable()) {
				selectedId = ck.getId().toString();
				selectedText = ck.getKeyValue();
				update.markAttributeUpdate("selectedId", "value");
				update.markAttributeUpdate("selectedText", "value");
			}
		}
	}

	@Action(id = "ckSearch")
	public void searchDept() {
		if (ck_txt != null && !"".equals(ck_txt)) {
			searchMap = commonKeyService.getSearchMap(ck_txt, keyType);
		}
		cks_selectTree.setRootVisible(false);
		cks_selectTree.setAutoScroll(true);
		cks_selectTree.repaint();
	}

	@BeforeRender
	public void beforeRender(boolean isPostback) {
		if (!isPostback) {
			keyType = context.getExternalContext().getRequestParameterMap()
					.get("keyType");
			logger.debug("keyType is:" + keyType);
			ck_txt = null;
			searchMap = null;
		}
	}
}
